using UnityEngine;
using System.Collections;

public class Missile : MonoBehaviour {
	
	public float speed;
	private int damage;
	public float rotationSpeed = 0.3f;
	private GameObject target;
	
	public void setup (GameObject target, int dmg)
	{
		this.damage = dmg;
		this.target = target;
	}
	
	void Start () 
	{
		
	}
	
	void Update () 
	{
		RotateTo(target.transform.position);
		transform.Translate(Vector3.forward * speed * Time.deltaTime);
		
	}
	
	void RotateTo (Vector3 targetPosition)
	{
		Quaternion finalRotation;
		Vector3 relativePos;
		relativePos = targetPosition - transform.position;
		finalRotation = Quaternion.LookRotation(relativePos);	
		transform.rotation = Quaternion.Slerp(transform.rotation, finalRotation, rotationSpeed);
	}
	
	void OnTriggerEnter (Collider other) 
	{
		if (other.CompareTag("enemy1"))
		{
			Destroy (gameObject);
			Enemy temp = target.GetComponent<Enemy>();
			temp.Hit(damage);
		}
	}
}
